//==========================================================================//
//                                                                          //
//  PACHUBE Library             Version 0.1                     March 2012  //
//                                                                          //
//  Written for the Arduino Duemilanove w/ ATmega328                        //
//                                                                          //
//  PACHUBE Arduino Library                                                 //
//                                                                          //
//  Created by JFLagarde (jeanfrancoislagarde@hotmail.com)                  //
//                                                                          //
//  This file is part of OpenWS (http://code.google.com/p/openws/)          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Notes:                                                                  //
//                                                                          //
//                                                                          //
//==========================================================================//
//                                                                          //
//  Copyright 2012 Jean-Francois Lagarde (jeanfrancoislagarde@hotmail.com)  //
//  This program is distributed under the terms of                          //
//  the GNU General Public License                                          //
//                                                                          //
//  This program is free software: you can redistribute it and/or modify    //
//  it under the terms of the GNU General Public License as published by    //
//  the Free Software Foundation, either version 3 of the License, or       //
//  (at your option) any later version.                                     //
//                                                                          //
//  This program is distributed in the hope that it will be useful,         //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//  GNU General Public License for more details.                            //
//                                                                          //
//  You should have received a copy of the GNU General Public License       //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                          //
//==========================================================================//

#include <Ethernet.h>
#include <PACHUBE.h>

PACHUBE::PACHUBE() {
	EthernetClient _clientPachube;
}

void PACHUBE::init() {
	char* apiKey = "MYAPIKEY"; // replace your pachube api key here
	unsigned int localFeed = MYFEED;   // replace your pachube feed number here
	init(apiKey, localFeed);
}
void PACHUBE::init(char* apiKey, unsigned int localFeed) {
	_apiKey = apiKey;
	_localFeed = localFeed;
}
void PACHUBE::send(char* id1, int value1, char* id2, int value2, char* id3, int value3, char* id4, int value4, char* id5, int value5, char* id6, int value6) {
  // TODO: ID and Value arrays
  
  //if (_clientPachube.connect("www.pachube.com",80)) {  
  if (_clientPachube.connect("api.pachube.com",80)) {  
	  //_clientPachube.print("PUT /api/feeds/");
	  _clientPachube.print("PUT /v2/feeds/");
    _clientPachube.print(_localFeed);
    _clientPachube.print(".csv HTTP/1.1\n");
    //_clientPachube.print("Host: www.pachube.com\n");
	_clientPachube.print("Host: api.pachube.com\n");
    _clientPachube.print("X-PachubeApiKey: ");
    _clientPachube.print(_apiKey);
    _clientPachube.print("\n");
    _clientPachube.print("Content-Length: ");
    int thisLength = strlen(id1) + getLength(value1) + strlen(id2) + getLength(value2) + strlen(id3) + getLength(value3) + strlen(id4) + getLength(value4) + strlen(id5) + getLength(value5) + strlen(id6) + getLength(value6) + 6+6;
    _clientPachube.println(thisLength, DEC);
    
    // last pieces of the HTTP PUT request:
    _clientPachube.print("Content-Type: text/csv\n");
    _clientPachube.println("Connection: close\n");

    // here's the actual content of the PUT request:
    _clientPachube.print(id1); 
	_clientPachube.print(","); 
	_clientPachube.print(value1); 
    _clientPachube.print("\n"); 
	_clientPachube.print(id2);
	_clientPachube.print(",");
    _clientPachube.print(value2); 
	_clientPachube.print("\n");
	_clientPachube.print(id3); 
	_clientPachube.print(","); 
	_clientPachube.print(value3);
	_clientPachube.print("\n");
	_clientPachube.print(id4); 
	_clientPachube.print(","); 
	_clientPachube.print(value4);
	_clientPachube.print("\n");
	_clientPachube.print(id5); 
	_clientPachube.print(","); 
	_clientPachube.print(value5);
	_clientPachube.print("\n");
	_clientPachube.print(id6); 
	_clientPachube.print(","); 
	_clientPachube.print(value6);
	_clientPachube.println();
	  
	_clientPachube.flush();
    _clientPachube.stop();
  } 
}









//PRIVATE methods

// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int PACHUBE::getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}